package com.thinkgem.jeesite.common.pojo.doc.xml;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

class XmlSAXHandler extends DefaultHandler {
	boolean hasAttribute = false;
	Attributes attributes = null;
	private String tagName = null;
	private HashMap<String,String> map = new HashMap();
	private ArrayList strlist = new ArrayList();

	public ArrayList getStrlist() {
		return strlist;
	}

	public void setStrlist(ArrayList strlist) {
		this.strlist = strlist;
	}

	int count = 0;
	
	public boolean isHasAttribute() {
		return hasAttribute;
	}

	public void setHasAttribute(boolean hasAttribute) {
		this.hasAttribute = hasAttribute;
	}

	public Attributes getAttributes() {
		return attributes;
	}

	public void setAttributes(Attributes attributes) {
		this.attributes = attributes;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public HashMap<String, String> getMap() {
		return map;
	}

	public void setMap(HashMap<String, String> map) {
		this.map = map;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public void startDocument() throws SAXException {
		System.out.println("文档开始打印了");
	}

	public void endDocument() throws SAXException {
		System.out.println("文档打印结束了");
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		this.tagName = qName;
//		if (qName.equals("model")) {
//			System.out.println(qName);
//			System.out.println(attributes.getQName(0)+':'+attributes.getValue(0));
//			System.out.println(attributes.getQName(1)+':'+attributes.getValue(1));
//		}
//		if (qName.equals("field")) {
//			System.out.println(qName);
//			System.out.println(attributes.getQName(0)+':'+attributes.getValue(0));
//			System.out.println(attributes.getQName(1)+':'+attributes.getValue(1));
//		}
//		if (qName.equals("query")
//				||qName.equals("insert")
//				||qName.equals("delete")
//				||qName.equals("update")) {
//			System.out.println(qName);
//			System.out.println(attributes.getQName(0)+':'+attributes.getValue(0));
//		}
		if (attributes.getLength() > 0) {
			this.attributes = attributes;
			this.hasAttribute = true;
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
//		if (hasAttribute && (attributes != null)) {
//			for (int i = 0; i < attributes.getLength(); i++) {
//				System.out.println(
//						attributes.getLength() + ':' + 
//						attributes.getLocalName(i) + ':' + 
//						attributes.getQName(i) + ':' +
//						attributes.getValue(i));
//			}
//		}
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (tagName.equals("sql")){
			String sql = new String(ch, start, length);
			sql = sql.trim();
			strlist.add(sql);
			//map.put(tagName, sql);
//			attributes.getValue(tagName);
//			System.out.println(attributes.getValue(1));
//			System.out.println(attributes.getValue(2));
//			System.out.println(attributes.getValue(3));
//			//System.out.println(new String(ch, start, length));
		}
	}
}